package com.huawei.demo;

import com.huaweicloud.sdk.codeartsartifact.v2.CodeArtsArtifactClient;
import com.huaweicloud.sdk.codeartsartifact.v2.model.ReleaseFileVersionDo;
import com.huaweicloud.sdk.codeartsartifact.v2.model.ShowProjectReleaseFilesRequest;
import com.huaweicloud.sdk.codeartsartifact.v2.model.ShowProjectReleaseFilesResponse;
import com.huaweicloud.sdk.core.auth.BasicCredentials;
import com.huaweicloud.sdk.core.auth.ICredential;
import com.huaweicloud.sdk.core.exception.ConnectionException;
import com.huaweicloud.sdk.core.exception.RequestTimeoutException;
import com.huaweicloud.sdk.core.exception.ServiceResponseException;
import com.huaweicloud.sdk.core.http.HttpConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class ShowProjectReleaseFilesDemo {
    private static final Logger LOGGER = LoggerFactory.getLogger(ShowProjectReleaseFilesDemo.class.getName());

    public static void main(String[] args) {
        HttpConfig config = HttpConfig.getDefaultHttpConfig().withIgnoreSSLVerification(true);
        ICredential auth = new BasicCredentials().withAk("<YOUR AK>").withSk("<YOUR SK>")
                .withProjectId("<YOUR REGION PROJECT ID>");
        CodeArtsArtifactClient client = CodeArtsArtifactClient.newBuilder().withCredential(auth)
                .withEndpoint("<YOUR URL>").withHttpConfig(config).build();
        ShowProjectReleaseFilesRequest request = new ShowProjectReleaseFilesRequest();
        request.setProjectId("<YOUR FILE PROJECT ID>");
        request.setFileName("<YOUR FILE NAME>");
        request.setOffset(0);
        request.setLimit(10);
        try {
            // 请求查询文件信息
            ShowProjectReleaseFilesResponse response = client.showProjectReleaseFiles(request);
            List<ReleaseFileVersionDo> releaseFileVersionDoList = response.getResult().getData();
            for (int i = 0; i < releaseFileVersionDoList.size(); i++) {
                String filePath = releaseFileVersionDoList.get(i).getPath();
                String version = releaseFileVersionDoList.get(i).getVersion();
                String downloadUrl = releaseFileVersionDoList.get(i).getDownloadUrl();
                // 获取文件信息
                LOGGER.info("FilePath:{}, Version:{}, DownloadUrl:{}", filePath, version, downloadUrl);
            }
        } catch (ConnectionException e) {
            LOGGER.error(e.getMessage());
        } catch (RequestTimeoutException e) {
            LOGGER.error(e.getMessage());
        } catch (ServiceResponseException e) {
            LOGGER.error(e.getMessage());
            LOGGER.error(String.valueOf(e.getHttpStatusCode()));
            LOGGER.error(e.getErrorCode());
            LOGGER.error(e.getErrorMsg());
        }
    }
}